package org.bonorsoft.modules.control.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bonorsoft.modules.control.entity.Address;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.bonorsoft.modules.control.entity.DoorCode;
import org.bonorsoft.modules.control.vo.AddressVo;
import org.bonorsoft.modules.control.vo.QrcodeVo;

import java.util.List;

/**
 * @Description: 走访地址
 * @Author: WilliamYe
 * @Date:   2021-01-30
 * @Version: V1.0
 */
public interface AddressMapper extends BaseMapper<Address> {

    @Select("SELECT d.dzbm,d.havefw,d.zdlx,d.ryxx,d.dwxx,d.gxsj,d.dzmc,d.sssj,d.ssfj,d.sspcs,d.ssjwq,d.dzlx,d.ssjlxxq_dzbm,d.cjsj,d.cxsj,d.police_name," +
            "(select t.template_id from police_info_template t where t.info_id = d.dzbm) as template_id, " +
            "(select DISTINCT r.address_id from police_check_result r where r.address_id = d.dzbm) as result_id " +
            "FROM ybss_bzdz_dzzf d ${ew.customSqlSegment}")
    IPage<AddressVo> paging(Page<Address> page, @Param(Constants.WRAPPER) QueryWrapper<Address> queryWrapper);

    @Insert("insert into police_door_qrcode (dzbm, qrcode, link)values(#{dzbm}, #{qrcodeStr}, #{link})")
    int insertQrcode(String dzbm, String qrcodeStr, String link);

    @Select("SELECT d.dzbm,d.havefw,d.dzmc," +
            "(select t.link from police_door_qrcode t where t.dzbm = d.dzbm) as haveQrcode " +
            "FROM ybss_bzdz_dzzf d ${ew.customSqlSegment}")
    IPage<DoorCode> pageCode(Page<Address> page, @Param(Constants.WRAPPER) QueryWrapper<Address> queryWrapper);

    @Select("select id, dzbm, qrcode, link form police_door_qrcode where dzbm = #{dzbm}")
    int countQrcode(String dzbm);

    @Select("select qrcode from police_door_qrcode where dzbm = #{dzbm}")
    String getQrcode(String dzbm);

    @Select("select y.dzmc as dzmc, p.link as qrcode from police_door_qrcode p, ybss_bzdz_dzzf y " +
            "where p.dzbm = y.dzbm")
    List<QrcodeVo> getAllQrocde();

}
